//
//  WLYViewController.swift
//  iiatn
//
//  Created by mine on 18/4/26.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD

class WLYViewController: UIViewController, UITextViewDelegate{

    @IBOutlet weak var liuyan: UITextView!
    
    @IBOutlet weak var email: UIButton!
    
    @IBOutlet weak var tel: UIButton!
    
    @IBOutlet weak var name: UITextView!
    
    @IBOutlet weak var contact: UITextView!
    
    @IBOutlet weak var content: UITextView!
    
    @IBOutlet weak var goly: UIButton!
    
    @IBOutlet weak var hang1: UIButton!
    
    @IBOutlet weak var hang2: UIButton!
    
    
    @IBOutlet var scroll: UIScrollView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        name.delegate = self
        contact.delegate = self
        content.delegate = self
        
        self.view.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(self.handleTap(sender:))))

        NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillShow(_notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillHide(_notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
        
        setstyle()
        setdosth()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func setstyle(){
        scroll.width = SCREENW
        scroll.height = SCREENH - 20
        
        hang1.width = SCREENW
        hang2.width = SCREENW
        goly.width = SCREENW - 20
        
        liuyan.x = (SCREENW - liuyan.width)/2
        liuyan.layer.cornerRadius = liuyan.height * 0.03
        liuyan.layer.masksToBounds = true
        liuyan.layer.rasterizationScale = UIScreen.main.scale
        
        name.layer.borderWidth = 0.6
        name.layer.borderColor = UIColor.gray.cgColor
        name.width = SCREENW - 68
        name.layer.cornerRadius = name.height * 0.1
        name.layer.masksToBounds = true
        name.layer.rasterizationScale = UIScreen.main.scale
        name.text = "请输入联系人或者公司名"
        name.textColor = UIColor.lightGray
        name.tag = 1
        
        contact.layer.borderWidth = 0.6
        contact.layer.borderColor = UIColor.gray.cgColor
        contact.width = SCREENW - 68
        contact.layer.cornerRadius = contact.height * 0.1
        contact.layer.masksToBounds = true
        contact.layer.rasterizationScale = UIScreen.main.scale
        contact.text = "请输入手机或微信或QQ等联系方式"
        contact.textColor = UIColor.lightGray
        contact.tag = 2
        
        content.layer.borderWidth = 0.6
        content.layer.borderColor = UIColor.gray.cgColor
        content.width = SCREENW - 68
        content.layer.cornerRadius = content.height * 0.06
        content.layer.masksToBounds = true
        content.layer.rasterizationScale = UIScreen.main.scale
        content.text = "请输入留言内容"
        content.textColor = UIColor.lightGray
        content.tag = 3
    }
    
    func setdosth(){
        email.addTarget(self, action: #selector(copyemail), for: .touchUpInside)
        tel.addTarget(self, action: #selector(copytel), for: .touchUpInside)
        goly.addTarget(self, action: #selector(tryly), for: .touchUpInside)
    }
    
    
    func copyemail(){
        UIPasteboard.general.string = "nnhzcjh@sina.com"
        SVProgressHUD.showInfo(withStatus: "邮箱 nnhzcjh@sina.com 已复制到粘贴板")
    }
    func copytel(){
        UIPasteboard.general.string = "01067171008"
        SVProgressHUD.showInfo(withStatus: "服务热线 01067171008 已复制到粘贴板")
    }
    func tryly(){
        var trygo = true
        if name.textColor == UIColor.lightGray {
            trygo = false
            SVProgressHUD.showError(withStatus: "名称不能为空！")
            return
        }
        if contact.textColor == UIColor.lightGray {
            trygo = false
            SVProgressHUD.showError(withStatus: "联系不能为空！")
            return
        }
        if content.textColor == UIColor.lightGray || content.text == nil{
            trygo = false
            SVProgressHUD.showError(withStatus: "留言不能为空！")
            return
        }
        
        if trygo {
            doly()
        }
    }
    
    func doly(){
        var mphone = ""
        var mcontact = contact.text!
        if (Validate.phoneNum(contact.text!).isRight) {
            mphone = contact.text!
            mcontact = ""
        }
        WNetwork.shareNetworkTool.LiuYan(name: name.text!, phone: mphone, contact: mcontact, content: content.text!){ (msg) in
            print("liuyan success!")
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.8) {self.navigationBackClick()}
        }
    }
    /// 返回按钮
    func navigationBackClick() {
        let back_now = navigationController?.popViewController(animated: true)
        print("发布项目后返回：\(back_now)")
    }
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = nil
            textView.textColor = UIColor.black
        }
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            if textView.tag == 1 {
                textView.text = "请输入联系人或者公司名"
            }else if textView.tag == 2{
                textView.text = "请输入手机或微信或QQ等联系方式"
            }else {
                textView.text = "请输入留言内容"
            }
            textView.textColor = UIColor.lightGray
        }
    }
    
    func keyBoardWillShow(_notification: Notification){
        //获取userInfo
        let kbInfo = _notification.userInfo
        //获取键盘的size
        let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        //键盘的y偏移量
        let changeY = kbRect.origin.y - SCREENH
        //键盘弹出的时间
        let duration = kbInfo?[UIKeyboardAnimationDurationUserInfoKey] as!Double
        
        //界面偏移动画
        UIView.animate(withDuration: duration) {
            
            self.scroll.transform = CGAffineTransform(translationX: 0, y:changeY)
        }
    }
    
    //键盘的隐藏
    func keyBoardWillHide(_notification: Notification){
        
        let kbInfo = _notification.userInfo
        //let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        /*
         *
         let kbRect = (userInfo![UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
         *
         swift2.3正常，swift3.0取值为nil
         */
        //let changeY = kbRect.origin.y
        let duration = kbInfo?[UIKeyboardAnimationDurationUserInfoKey] as!Double
        
        UIView.animate(withDuration: duration) {
            self.scroll.transform = CGAffineTransform(translationX: 0, y:0)
            //self.scroll.update(inset:0)
        }
    }
    
    func handleTap(sender: UITapGestureRecognizer) {
        if sender.state == .ended {
            name.resignFirstResponder()
            contact.resignFirstResponder()
            content.resignFirstResponder()
        }
        sender.cancelsTouchesInView = false
    }
    
    deinit {
        //移除通知
        NotificationCenter.default.removeObserver(self)
    }
}
